﻿
@{
    ViewBag.Title = "Add";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div style="padding: 16px">

    <div class="layui-card" style="height: 800px;">
        <div class="layui-card-body" style="height: max-content;">
            <h3>表单</h3>
            <table class="layui-hide" id="test2" lay-filter="test2"></table>
        </div>


        <form class="layui-form" action="" style="margin-left: 80px;margin-right: 80px;" lay-filter="user">
            <div class="layui-form-item" style="display: flex;flex-wrap: wrap;">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-inline">
                    <input type="tel" name="ParturientName" autocomplete="off" class="layui-input" style="height: 30px;">
                </div>
                <label class="layui-form-label">年龄</label>
                <div class="layui-input-inline">
                    <input type="tel" name="ParturientAge" autocomplete="off" class="layui-input" style="height: 30px;">
                </div>
                <label class="layui-form-label">电话</label>
                <div class="layui-input-inline">
                    <input type="tel" name="ParturientPhone" autocomplete="off" class="layui-input" style="height: 30px;">
                </div>
            </div>
            <div class="layui-form-item" style="display: flex;flex-wrap: wrap;">
                <label class="layui-form-label">家庭地址</label>
                <div class="layui-input-inline">
                    <input type="tel" name="Address" autocomplete="off" class="layui-input" style="height: 30px;">
                </div>
                <label class="layui-form-label">学历</label>
                <div class="layui-input-inline">
                    <select id="DegreeSelect" class="layui-input" lay-ignore>
                        <option value="保密">保密</option>
                        <option value="本科">本科</option>
                        <option value="专科">专科</option>
                        <option value="硕士">硕士</option>
                        <option value="博士">博士</option>
                    </select>
                </div>
                <input type="hidden" name="Degree" id="DegreeId" value="">
                <label class="layui-form-label">紧急联系人</label>
                <div class="layui-input-inline">
                    <input type="tel" name="EmergencyContacts" autocomplete="off" class="layui-input" style="height: 30px;">
                </div>
            </div>
            <div class="layui-form-item" style="display: flex;flex-wrap: wrap;">

                <label class="layui-form-label">紧急联系人电话</label>
                <div class="layui-input-inline">
                    <input type="tel" name="EmergencyContactsPhone" autocomplete="off" class="layui-input" style="height: 30px;">
                </div>
                <label class="layui-form-label">围产医院</label>
                <div class="layui-input-inline">
                    <input type="tel" name="PerinatalHospitals" autocomplete="off" class="layui-input" style="height: 30px;">
                </div>
                <label class="layui-form-label">预产期</label>
                <div class="layui-input-inline">
                    <input type="tel" name="BirthTime" id="date" autocomplete="off" class="layui-input" style="height: 30px;">
                </div>
                @*<label class="layui-form-label">业务员</label>
        <div class="layui-input-inline">
            <input type="tel" name="Salesman" autocomplete="off" class="layui-input" style="height: 30px;">
        </div>*@
            </div>
            @*<div class="layui-form-item" style="display: flex;flex-wrap: wrap;">
                <label class="layui-form-label">业务员工号</label>
                <div class="layui-input-inline">
                    <input type="tel" name="SalesmanNumbers" autocomplete="off" class="layui-input" style="height: 30px;">
                </div>
                <label class="layui-form-label">预产期</label>
                <div class="layui-input-inline">
                    <input type="tel" name="BirthTime" id="date" autocomplete="off" class="layui-input" style="height: 30px;">
                </div>
                <label class="layui-form-label">上次联系</label>
                <div class="layui-input-inline">
                    <input type="tel" name="EntryTime" id="date1" autocomplete="off" class="layui-input" style="height: 30px;">
                </div>
            </div>*@
            <div class="layui-form-item" pane="">
                <label class="layui-form-label">意向信息</label>
                <div class="layui-input-block">
                    <div>
                        <input type="checkbox" id="checkbox_a" lay-skin="primary" title="产妇入住意向强烈" value="3">@*加checked=""默认选中*@
                        <input type="checkbox" id="checkbox_b" lay-skin="primary" title="产妇家人强烈支持" value="3">
                        <input type="checkbox" id="checkbox_c" lay-skin="primary" title="产妇生活品质要求高" value="1">@*加disabled=""属性则复选框变暗为不可选中*@
                        <input type="checkbox" id="checkbox_d" lay-skin="primary" title="产妇家庭收入较高" value="2">
                    </div>
                    <div>
                        <input type="checkbox" id="checkbox_e" lay-skin="primary" title="产妇不考虑入住" value="-3">
                        <input type="checkbox" id="checkbox_f" lay-skin="primary" title="产妇觉得收费较贵" value="-1">
                        <input type="checkbox" id="checkbox_g" lay-skin="primary" t title="产妇已在其他地方购买" value="-2">
                    </div>
                </div>
            </div>
            <div class="layui-form-item" style="width: 1000px; display: flex; justify-content: space-between; align-items: center">
                <div style="flex: 1; display: flex; justify-content: flex-start">
                    <div>
                        <p style="margin-left: 60px">总权重：<span id="total-weight">0</span></p>
                    </div>
                </div>
                <div style="flex: 1; display: flex; justify-content: center">
                </div>
                <div style="flex: 1; display: flex; justify-content: flex-end">
                </div>
            </div>
            <input type="hidden" id="WeightDetail" value="" name="WeightDetail" />
            <input type="hidden" id="Weight" value="" name="Weight" />
            @*<div class="layui-form-item" style="display: flex;flex-wrap: wrap;">

                    <label class="layui-form-label">前台ID</label>
                    <div class="layui-input-inline">
                        <input type="tel" name="Reception" autocomplete="off" class="layui-input" style="height: 30px;">
                    </div>
                </div>*@
            <input type="hidden" id="hfname" value="@ViewBag.ParturientName" />
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="q">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    //Demo
    var form;
    var hfname;
    var util;
    var weigtlist = [];
    var totalWeight = 0; // 初始化总权重
    var selectedItems = []; // 用来保存选中的复选框项的ID或其他标识
    window.onload = function () {
        layui.use(['form', 'util', 'laydate', 'upload'], function () {
            form = layui.form;
            var layer = layui.layer;
            util = layui.util;
            var laydate = layui.laydate;
            var upload = layui.upload;
            var $ = layui.jquery;
            hfname = $('#hfname').val();
            // console.log($('#hfname').val());
            if (hfname != null) {

                init();
            }
            // form.render(); //刷新select选择框渲染
            //日期
            laydate.render({
                elem: '#date'
            });
            laydate.render({
                elem: '#date1'
            });
            //监听提交
            form.on('submit(q)', function (data) {
                //console.log(data.field); //当前容器的全部表单字段，名值对形式：{name: value}
                //console.log(data.field); //当前容器的全部表单字段，名值对形式：{name: value}
                $('#DegreeId').val($('#DegreeSelect').val());
                data.field.Degree = $('#DegreeId').val();
                $('#WeightDetail').val(selectedItems);
                data.field.WeightDetail = $('#WeightDetail').val();
                $('#Weight').val(totalWeight);
                data.field.Weight = $('#Weight').val();
                if (hfname != null) {
                    //更新
                    update(data);
                } else {
                    //添加
                    add(data);
                }

                return false;
            });
            //初始化

            // 监听复选框变化事件
            form.on('checkbox', function (data) {
                //console.log(data.elem.id)
                // 获取当前复选框的权重
                debugger
                var weight = parseInt(data.elem.value);
                var itemId = data.elem.id;  // 假设复选框有一个 dataset 属性保存项的ID
                // 计算选中或取消选中的权重变化
                if (data.elem.checked) {
                    totalWeight += weight; // 选中时加权重
                    selectedItems.push(itemId);  // 保存选中的项ID
                } else {
                    totalWeight -= weight; // 取消选中时减权重
                    selectedItems = selectedItems.filter(id => id !== itemId); // 从选中项中移除该ID
                }
                // 更新显示总权重
                document.getElementById('total-weight').textContent = totalWeight;
            });
        });


    };
    function init() {
         debugger
        $.post("/Parturient/userget?name=" + hfname, function (resp) {
            console.log(resp.data)
            if (resp.code == 0) {
                if (resp.data.BirthTime != null)
                    resp.data.BirthTime = util.toDateString(resp.data.BirthTime.replace('/Date(', '').replace(')/', ''), 'yyyy-MM-dd');
                if (resp.data.EntryTime != null)
                    resp.data.EntryTime = util.toDateString(resp.data.EntryTime.replace('/Date(', '').replace(')/', ''), 'yyyy-MM-dd');
                form.val('user', resp.data)
                totalWeight = resp.data.Weight;
                document.getElementById('total-weight').textContent = totalWeight;
                selectedItems = resp.data.WeightDetail.split(','); // 假设 WeightDetail 是以逗号分隔的字符串
                $('#DegreeSelect').val(resp.data.Degree);
                console.log(resp.data.WeightDetail)
                $('input[type="checkbox"]').each(function () {
                    if (resp.data.WeightDetail.includes(this.id)) {
                        this.checked = true;
                    }
                });

                //form.render(); //刷新select选择框渲染
            }
        })
    }
    //更新
    function update(data) {
        debugger
        $.get("/Parturient/Update?name=" + hfname, { data: JSON.stringify(data.field) }, function (resp) {
            if (resp.code == 0) {
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index);
                parent.layui.table.reload('testpa');
            }
        });
    }

    //添加
    function add(data) {
        $.get("/Parturient/Insert", { data: JSON.stringify(data.field) }, function (resp) {
            if (resp.code == 0) {
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index);
                parent.layui.table.reload('testpa');
            }
        });
    }
</script>

